from django.http import HttpResponse
from django.shortcuts import render
from django.db import DatabaseError,DataError
from .models import *
import logging
# Create your views here.

def getform(request):
    return render(request, 'message_form.html')

new_one = UserMssage()
def insertdata(request):
    if request.method == "POST":
        new_one.name = request.POST.get('name')
        new_one.email = request.POST.get('email')
        new_one.address = request.POST.get('address')
        new_one.message = request.POST.get('message')
        try:
            new_one.save()
        except DatabaseError as e:
            logging.warning(e)
            return HttpResponse("留言失败！")
        return HttpResponse("留言成功！")
def getmessage(request):
    message = None
    all_messages = UserMssage.objects.filter(name='ayjamal')
    if all_messages:
        message = all_messages[0]
    return render(request,'messages.html',{"mymessage":message})